
 
import UIKit

class MYLoginBaseTextFieldCodeCell: MYLoginBaseTextFieldCell {
    @objc lazy var codeBtn: UIButton = {
        let codeBtn = UIButton(type: .custom);
        codeBtn.setTitleColor(UIColorFromHex(rgbValue: 0xCBCBCB), for: .normal)
        codeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        codeBtn.setTitle(kYQLSSwift("发送验证码"), for: .normal)
        codeBtn.frame = CGRect(x: 0, y: 0, width: 62, height: 21)
//        codeBtn .addTarget(self, action:Selector(("sendCodeClick:")), for: UIControl.Event.touchUpInside)
        return codeBtn;
    }()

    override func layoutSubviews() {
        super.layoutSubviews();
        self.leftIconImage = UIImage(named: "Login_Code_icon");
        self.textField.rightView = codeBtn;
        self.textField.keyboardType = UIKeyboardType.phonePad;
        self.textField.autocapitalizationType = UITextAutocapitalizationType.none;
//        self.textField.isSecureTextEntry = true;

        self.textField.rightViewMode = .always;
        //监听输入框的文本长度
        self.textField.addTarget(self, action:Selector(("textFieldDidChange:")), for: UIControl.Event.editingChanged)

    }
    
    @objc func textFieldDidChange(_ textField: UITextField) {
        guard let _ = textField.text else { return }
        
        debugPrint("\(textField.text!)")
        let lang = textField.textInputMode?.primaryLanguage
        let tmpText = textField.text!
        if lang == "zh-Hans" {
            let range = textField.markedTextRange
            if range == nil {
                guard tmpText.count >= 6 else { return }
                textField.text = tmpText.mySubString(to: 6)
            }
        } else {
            guard tmpText.count >= 6 else { return }
            textField.text = tmpText.mySubString(to: 6)
        }
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
//
//    @objc func sendCodeClick(_ button: UIButton) {
//        
//    }
}

